<?php
/**
 * Created by PhpStorm.
 * User: lenvo
 * Date: 2018/7/21
 * Time: 13:59
 */

namespace app\home\controller;


use think\Loader;

class Swiper extends Basic
{
    protected $swiper = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->three_menu[0]['text'] = '列表';
        $this->three_menu[0]['url']  = 'index';
        $this->swiper = Loader::model("Swiper");
        $this->assign('third_menu', $this->three_menu);
    }

    public function index() {
        $records = $this->swiper->where("valid",1)->select();
        $this->assign("records",$records);
        return $this->fetch();
    }

    public function add() {
        if($this->request->isPost()) {
            $_form = input('');
            $res = $this->swiper->save($_form);
            if($res) {
                return $this->ret->setCode(0)->setMsg("保存成功")->setReload()->toJson();
            }else {
                return $this->ret->setCode(1)->setMsg("保存失败")->toJson();
            }
        }else {
            return $this->fetch();
        }
    }

    public function edit() {
        if($this->request->isPost()) {
            $id = input('id');
            $_form = input('');
            $res = $this->swiper->save($_form,['id'=>$id]);
            if($res) {
                return $this->ret->setCode(0)->setMsg("修改成功")->setReload()->toJson();
            }else {
                return $this->ret->setCode(1)->setMsg("修改失败")->toJson();
            }
        }else {
            $id = input('id');
            $one = $this->swiper->where("id",$id)->find();
            $this->assign("one",$one);
            return $this->fetch();
        }
    }

    public function del() {
        $id = input("id");
        $res = $this->swiper->save(['valid' => 0],['id' => $id]);
        if($res) {
            return $this->ret->setCode(0)->setMsg("删除成功")->toJson();
        }else {
            return $this->ret->setCode(1)->setMsg("删除失败")->toJson();
        }
    }
}